﻿using System.Collections.Generic;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Cms.Tests.Unit.Application.Admin.Presenters
{
    [TestClass]
    public class UsersPresenterTests : BasePresenterTests
    {
        private Mock<IUsersView> _viewMock;
        private IList<UserGroup> _userGroups;
        private IList<User> _users;

        private void Prepare()
        {
            this.InitRepositoryFactoryMock();
            UserGroup VIPs = new UserGroup();
            UserGroup members = new UserGroup();
            _userGroups = new List<UserGroup> { members, VIPs };
            UserRepositoryMock.Setup(m => m.GetUserGroups()).Returns(_userGroups);
            _viewMock = new Mock<IUsersView>();
            _viewMock.SetupAllProperties();
            _viewMock.Setup(v => v.AdministratorID).Returns(1);
            AdministratorGroup admins = new AdministratorGroup() { ID = 1 };
            Administrator admin = new Administrator {Group = admins};
            AdministratorRepositoryMock.Setup(m => m.GetAdministrator(1)).Returns(admin);
        }

        [TestMethod]
        public void SetUserGroups_FirstTime_AllGroupsInList()
        {
            Prepare();
            Assert.AreEqual(2, UserRepositoryMock.Object.GetUserGroups().Count);
        }

        [TestMethod]
        public void Initialize_FirstTime_AllGroupsInList()
        {
            Prepare();
            UsersPresenter presenter = new UsersPresenter(_viewMock.Object, FactoryMock.Object);
            presenter.Initialize();
            _viewMock.Verify(m => m.SetUserGroups(_userGroups));
            _viewMock.VerifySet(m => m.SelectedUserGroupId = -1, Times.Exactly(1));
        }

        [TestMethod]
        public void SearchUsers_NoFilterApplied_ShowAll()
        {
            Prepare();
            //User a = new User();
            //User b = new User();
            //User c = new User();
            //_users.Add(a);
            //_users.Add(b);
            //_users.Add(c);
            UsersPresenter presenter = new UsersPresenter(_viewMock.Object, FactoryMock.Object);
            UserRepositoryMock.Setup(m => m.SearchUsers()).Returns(_users);
            presenter.SearchUsers();
            _viewMock.Verify(m => m.SetUsers(_users));
        }
    }
}
